@isTest
private class CodeCoverageUtil
{
    static testMethod void testMethod1() 
    {
    	test.startTest();
    	Template__c tmp = new Template__c(name = 'TTT');
    	insert tmp;
    	Template__c[] tempT = [select name from Template__c where id = :tmp.id];
    	system.assertEquals(tmp.name, tempT[0].name);

        Attachment a = new Attachment(name = 'Test Attach.doc', Body = Blob.valueOf('<html><body>This is {Attachment}</body></html>'), parentId = tmp.Id);
        insert a;
        //Id of the Attachment which is not used to create Plan Docs
        Attachment[] aT = [select Id from Attachment a where a.ParentId = :tmp.id];
        if(aT.size() > 0)
            delete aT;

        
        Template__c tName = new Template__c(Name = 'Sample');
        insert tName;
      	tempT = [select name from Template__c where id = :tName.id];
    	system.assertEquals(tName.name, tempT[0].name);

        Template__c[] t = [select Id from Template__c t where t.Id = :tmp.id];
        if(t.size() > 0)
        {
            delete t;
        }
  		Sales_Contract__c sc1 = new Sales_Contract__c();
  		insert sc1;
  		Sales_Contract__c[] scT = [select id from Sales_Contract__c where id = :sc1.id];
  		system.assertEquals(sc1.id, scT[0].id);
  		
  		Plan_Document_Inquiry__c pd1 = new Plan_Document_Inquiry__c(Plan_Document__c = sc1.id, InquiryStatus__c = 'Pending');
  		insert pd1;
  		Plan_Document_Inquiry__c[] pdT = [select InquiryStatus__c from Plan_Document_Inquiry__c where id = :pd1.id];
  		system.assertEquals(pd1.InquiryStatus__c, pdT[0].InquiryStatus__c);
  		
  		pdT[0].InquiryStatus__c = 'Inquiry Approved';
  		update pdT;
  		
  		Test.stopTest();
	}


    static testMethod void testMethod2() 
    {
    	test.startTest();
  		Sales_Contract__c sc1 = new Sales_Contract__c();
  		insert sc1;
  		Sales_Contract__c[] scT = [select id from Sales_Contract__c where id = :sc1.id];
  		system.assertEquals(sc1.id, scT[0].id);

  		Plan_Document_Inquiry__c pd1 = new Plan_Document_Inquiry__c(Plan_Document__c = sc1.id, InquiryStatus__c = 'Pending');
  		insert pd1;
  		Plan_Document_Inquiry__c[] pdT = [select InquiryStatus__c from Plan_Document_Inquiry__c where id = :pd1.id];
  		system.assertEquals(pd1.InquiryStatus__c, pdT[0].InquiryStatus__c);
  		pdT[0].InquiryStatus__c = 'Inquiry Rejected';
  		update pdT;

      	Test.stopTest();
	}

    static testMethod void testMethod3() 
    {
    	test.startTest();

  		CallidusDataSource__c cds = new CallidusDataSource__c();
  		insert cds;
        Attachment a = new Attachment(name = 'Test Attach3.txt', Body = Blob.valueOf('<html><body>This is {Attachment}</body></html>'), parentId = cds.Id);
        insert a;
		CallidusDataSource__c[] cdsList = [select Id, In_Use_Attachments__c from CallidusDataSource__c where id = :cds.id];
		system.assertEquals(cds.id, cdsList[0].id);
		cdsList[0].In_Use_Attachments__c = a.id;
		update cdsList;
		try
		{
			delete a;
		}
		catch(Exception e){}
		
		
		Remote_IP_Address__c[] rpList = [select id, Is_Role_Shown__c from Remote_IP_Address__c];
		delete rpList;
		Remote_IP_Address__c[] rpList2 = [select id, Is_Role_Shown__c from Remote_IP_Address__c];
		system.assertEquals(rpList2.size() ,0);
		
		Callidus_CommonUtilG.savePlanSearchConfigurations('yes', null, null, null, null);
		Callidus_CommonUtilG.savePlanSearchConfigurations('no', null, null, null, null);
      	Test.stopTest();
	}

    static testMethod void testMethod4() 
    {
    	Sales_Contract__c sc = new Sales_Contract__c(Status__c = 'Draft', UserId__c = UserInfo.getUserID(), Level1__c = UserInfo.getUserId(), name = 'TestPlanDoc '+Datetime.now());
    	insert sc;
    	Sales_Contract__c sctest = [select Status__c from Sales_Contract__c where id = :sc.id ];
    	system.assertEquals(sctest.Status__c, sc.Status__c);
    	sc.Status__c = 'Distributed';
    	update sc;
    	sctest = [select Status__c from Sales_Contract__c where id = :sc.id ];
    	system.assertEquals(sctest.Status__c, sc.Status__c);

	    Approval.ProcessSubmitRequest request =new Approval.ProcessSubmitRequest();
    	request.setObjectId(sc.Id);
        Approval.ProcessResult result = Approval.process(request);
    	
        ProcessInstance[] prInst = [Select Id from ProcessInstance where TargetObjectId = :sc.id and Status = 'Pending'];
        ProcessInstanceWorkitem[] prInstWItem;
        if(prInst.size() > 0)
    	{
        	prInstWItem = [select Id, OriginalActorId from ProcessInstanceWorkitem where ProcessInstanceId = :prInst[0].Id];
    	}
        PageReference pg = Page.Callidus_Plan_Approval_By_Manager;
        pg.getParameters().put('id', sc.id);
        pg.getParameters().put('pid', prInstWitem[0].id);
        
        Test.setCurrentPage(pg);
    	
    	Callidus_PlanApprovalRejectionByManager cpar = new Callidus_PlanApprovalRejectionByManager();
    	cpar.comments = 'Test';
    	cpar.approve();
    	
	}

    static testMethod void testMethod5() 
    {
    	Sales_Contract__c sc = new Sales_Contract__c(Status__c = 'Draft', UserId__c = UserInfo.getUserID(), Level1__c = UserInfo.getUserId(), name = 'TestPlanDoc '+Datetime.now());
    	insert sc;
    	Sales_Contract__c sctest = [select Status__c from Sales_Contract__c where id = :sc.id ];
    	system.assertEquals(sctest.Status__c, sc.Status__c);
    	sc.Status__c = 'Distributed';
    	update sc;
    	sctest = [select Status__c from Sales_Contract__c where id = :sc.id ];
    	system.assertEquals(sctest.Status__c, sc.Status__c);

	    Approval.ProcessSubmitRequest request =new Approval.ProcessSubmitRequest();
    	request.setObjectId(sc.Id);
        Approval.ProcessResult result = Approval.process(request);
    	
        ProcessInstance[] prInst = [Select Id from ProcessInstance where TargetObjectId = :sc.id and Status = 'Pending'];
        ProcessInstanceWorkitem[] prInstWItem;
        if(prInst.size() > 0)
    	{
        	prInstWItem = [select Id, OriginalActorId from ProcessInstanceWorkitem where ProcessInstanceId = :prInst[0].Id];
    	}
        PageReference pg = Page.Callidus_Plan_Approval_By_Manager;
        pg.getParameters().put('id', sc.id);
        pg.getParameters().put('pid', prInstWitem[0].id);
        
        Test.setCurrentPage(pg);
    	
    	Callidus_PlanApprovalRejectionByManager cpar = new Callidus_PlanApprovalRejectionByManager();
    	cpar.comments = 'Test';
    	cpar.reject();
    	
	}


}